package cn.zhuangcloud.newversion.login;

import cn.zhuangcloud.newversion.common.controller.BaseController;
import cn.zhuangcloud.newversion.common.kit.TokenKit;
import cn.zhuangcloud.newversion.common.validator.TokenValidator;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.ext.interceptor.GET;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.Ret;

@Clear(TokenValidator.class)
public class LoginController extends BaseController {

    private static final LoginService srv = LoginService.me;

    @Before({LoginInterceptor.class, GET.class})
    public void index() {
        renderTemplate("login.html");
    }

    @Before({POST.class, LoginValidator.class})
    public void doLogin() {
        String username = getPara("username");
        String password = getPara("password");

        Ret ret = srv.login(username, password);
        setCookie(TokenKit.COOKIEID, ret.getStr("token"), 3600);
        renderJson(ret);
    }
}
